package cn.wolfcode.shop.service;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.util.RedisConstants;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.net.Inet4Address;
import java.text.MessageFormat;
import java.util.List;
@Service@Transactional
public class CataLogServiceImpl implements ICataLogService {

    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IPropertyService propertyService;
    @Autowired
    private IProductService productService;

    public String SelectAllCache(){
        String data = (String) redisTemplate.opsForValue().get(RedisConstants.CATALOG_ALL);
        if (data == null) {
            data = refresh();
        }
        return data;
    }

    public String refresh(){
        String data = JSON.toJSONString(selectAll());
        redisTemplate.opsForValue().set(RedisConstants.CATALOG_ALL,data);
        return data;
    }

    @Override
    public List<Catalog> selectAll() {
        return catalogMapper.selectAll();
    }

    @Override
    public List<Catalog> getChildCatalog(Long catalogId) {
        List<Catalog> catalogs = catalogMapper.getChildCatalog(catalogId);
        String propertyCountKey; //属性数量key
        String productCountKey; //商品数量key
        Integer propertyCount;
        Integer productCount;
        for (Catalog catalog : catalogs) {
            //{}占位符
            propertyCountKey = MessageFormat.format(RedisConstants.CATALOG_PROPERTY_COUNT, catalog.getId());
            productCountKey = MessageFormat.format(RedisConstants.CATALOG_PRODUCT_COUNT, catalog.getId());
            propertyCount = (Integer) redisTemplate.opsForValue().get(propertyCountKey);
            productCount = (Integer) redisTemplate.opsForValue().get(productCountKey);
            if (propertyCount == null) {
                //如果没有缓存
                propertyCount = propertyService.selectPropertyCount(catalog.getId());
                redisTemplate.opsForValue().set(propertyCountKey, propertyCount);
            }
            if (productCount == null) {
                //如果没有缓存
                productCount = productService.selectProductCount(catalog.getId());
                redisTemplate.opsForValue().set(productCountKey, productCount);
            }
            //设置属性个数
            catalog.setPropertyCount(propertyCount);
            //设置商品个数
            catalog.setProductCount(productCount);
        }
        return catalogs;
    }

    @Override
    public void updateSort(Long[] ids) {
        Catalog catalog;
        for (int i = 0; i <ids.length; i++){
            catalog = catalogMapper.selectByPrimaryKey(ids[i]);
            catalog.setSort(i + 1);
            catalogMapper.updateByPrimaryKey(catalog);
        }
        refresh();
    }

    @Override
    public void saveOrUpdate(Catalog catalog) {
        if (catalog.getId() != null) {
            catalogMapper.updateByPrimaryKey(catalog);
        } else {
            Catalog parentCatalog = catalogMapper.selectByPrimaryKey(catalog.getPId());
            if (!parentCatalog.isParent()) {
                //如果不是父节点
                parentCatalog.setParent(true);
                catalogMapper.updateByPrimaryKey(parentCatalog);
            }
            catalogMapper.insert(catalog);
        }
        refresh();
    }

    @Override
    public void delete(Long catalogId) {
        Catalog parentCatalog  = catalogMapper.selectParentCatalogByChildrenCatalogId(catalogId);
        int count = catalogMapper.selectChildrenCatalogCount(parentCatalog.getId());
        if (count == 1){
            parentCatalog.setParent(false);
            catalogMapper.updateByPrimaryKey(parentCatalog);
        }
        catalogMapper.deleteByPrimaryKey(catalogId);
        refresh();
    }
}
